//
//  PickeBrandViewController.m
//  OCUI
//
//  Created by login on 15-8-4.
//
//

#import "PickeBrandViewController.h"
#import "HttpClient.h"
@interface PickeBrandViewController ()<UITableViewDataSource,UITableViewDelegate,HttpClientDelegate>
{
    NSIndexPath	* lastIndexPath;

}
@property(nonatomic,copy)NSArray *brandData;
@end

@implementation PickeBrandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self initView];
    [self initData];
}
- (void)initData
{
    HttpClient *http = [[HttpClient alloc]init];
    http.delegate = self;
    
    NSString *host = @"http://192.168.3.62:8901/specialcar/queryvehiclebrand/1";
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"2" forKey:@"clienttype"];
    
    [http getRequestAsyWithProgress:self.view and:host and:dict requestType:GET];
    
}

- (void)sendRequestDataArray:(NSDictionary *)dict
{
    NSLog(@"11111%@",dict);
    
    self.brandData = dict[@"vehiclebrand"];
}

- (void)initView
{
    UIView *brandView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height/3 + 30)];
    [self.view addSubview:brandView];
    
    UILabel *brandLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    brandLabel.text = @"品牌选择";
    brandLabel.backgroundColor = [UIColor yellowColor];
    [brandView addSubview:brandLabel];
    
    UITableView *brandTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height/3 - 30)];
    brandTableView.dataSource = self;
    brandTableView.delegate = self;
    [brandView addSubview:brandTableView];
    
    UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height/3 - 30, 50, 30)];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:DEFAULTCOLOR forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(closeSelfWindow) forControlEvents:UIControlEventTouchUpInside];
    [brandView addSubview:cancelButton];
    
    UIButton *confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 50, self.view.frame.size.height/3 - 30, 50, 30)];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setTitleColor:DEFAULTCOLOR forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(closeSelfWindowAndGetData) forControlEvents:UIControlEventTouchUpInside];
    [brandView addSubview:confirmButton];


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.brandData.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    NSUInteger row = [indexPath row];
    NSUInteger oldRow = [lastIndexPath row];
    cell.accessoryType = (row == oldRow && lastIndexPath != nil) ?
    UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

    cell.textLabel.text = self.brandData[indexPath.row][@"brandname"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger newRow = [indexPath row];
    NSInteger oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
    if (newRow != oldRow)
    {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                    indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;

        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
                                    indexPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        
        lastIndexPath = [indexPath copy];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
